package com.jiyun.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiyun.entity.day01.Memberr;
import com.jiyun.entity.day02.*;
import com.jiyun.entity.day05.Compe;
import com.jiyun.entity.day05.CompeDto;
import com.jiyun.entity.day05.CompeVo;
import com.jiyun.entity.day06.*;
import com.jiyun.service.*;
import com.jiyun.utils.PermissionHelper;
import com.jiyun.vo.R;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.bouncycastle.asn1.x500.style.RFC4519Style;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.yaml.snakeyaml.events.Event;

import java.util.List;
import java.util.concurrent.TimeUnit;

import static org.bouncycastle.asn1.x500.style.RFC4519Style.*;

/*
 * 作者：LiuHaoyang
 * 时间：2023/8/23 15:59
 * 描述：
 **/
@RestController
@RequestMapping("homepage")
public class HomePageController {

    @Autowired
    private FeginService feginService;

    @Autowired
    private MemberrFeginService memberrFeginService;

    @Autowired
    private CompFeginService compFeginService;

    @Autowired
    private TrainnFeginService trainnFeginService;

    @Autowired
    private LessonnService lessonnService;

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private AnsService ansService;

    @Autowired
    private PaperrFeginService paperrFeginService;

    @Autowired
    private CouressFegin couressFegin;
    
    @Autowired
    private CatalogService catalogService;

    @Autowired
    private MuluService muluService;

    @Autowired
    private CompeService compeService;

    @Autowired
    private OptService optService;



    @GetMapping("add")
    public R add(Integer  optId , String da2){

        try {
            LambdaQueryWrapper<Ans> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Ans::getQueId, optId);
            Ans ans1 = ansService.getOne(wrapper);
            if (ans1==null){
                Ans ans = new Ans();
                ans.setQueAns(da2);
                ans.setQueId(optId);
                ansService.save(ans);
            }else {

                Ans ans = new Ans();
                ans.setId(ans1.getId());
                ans.setQueAns(da2);
                ans.setQueId(optId);
                ansService.updateById(ans);
            }
            return R.ok("ok");
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }

    }

    @GetMapping("optId")
    public R optId(Integer id ){
        try {
            Opt byId = optService.getById(id);
            return R.ok(byId);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @GetMapping("findOpt1")
    public R findOpt1(){
        try {
            List<Opt> list = optService.list();
            return R.ok(list);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }


    @GetMapping("findOpt")
    public R findOpt(OptDTO optDTO){
        try {
            PageHelper.startPage(optDTO.getPageNum(),optDTO.getPageSize());
            List<Opt> list = optService.findOpt(optDTO);
            PageInfo<Opt> pageInfo = new PageInfo<>(list);
            return R.ok(pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }



    @PostMapping("addCompe")
    public R addCompe(@RequestBody CompeVo compeVo){
        try {
//            Integer id1 = (Integer) redisTemplate.opsForValue().get("竞赛id");
//            compeVo.setId(id1);
//            compeService.updateById(compeVo);

            Integer id = (Integer) redisTemplate.opsForValue().get("竞赛id");
            compeVo.setId(id);
            compeVo.setStatus(0);
            compeVo.setJingdu("已报名");
            compeService.updateById(compeVo);
            return R.ok("ok");
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }


    @GetMapping("tiaoxq")
    public R tiaoxq(Integer id){
        redisTemplate.opsForValue().set("竞赛id",id);
        return R.ok("ok");
    }

    @GetMapping("getCompe")
    public R getCompe(){
        Integer id = (Integer) redisTemplate.opsForValue().get("竞赛id");
        Compe cmpeId = compeService.getById(id);
        return R.ok(cmpeId);
    }

    @GetMapping("findAllCompe")
    public R findCompe( CompeDto compeDto){

        try {
            PageHelper.startPage(compeDto.getPageNum(),compeDto.getPageSize());
            List<CompeVo> list = compeService.findCompe(compeDto);
            PageInfo<CompeVo> pageInfo = new PageInfo<>(list);
            return R.ok(pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }


    @GetMapping("xuexi")
    public R xuexi(Integer id){
        String user = (String) redisTemplate.opsForValue().get("user");
        Memberr memberr = JSON.parseObject(user, Memberr.class);
        Integer lessid = (Integer) redisTemplate.opsForValue().get("课程id");
        Mulu mulu = new Mulu();
        mulu.setMemberrId(memberr.getId());
        mulu.setLessonnId(lessid);
        mulu.setCatalogId(id);
        muluService.save(mulu);
        return R.ok("ok");
    }


    @GetMapping("geshu")
    public R geshu(){
        String user = (String) redisTemplate.opsForValue().get("user");
        Memberr memberr = JSON.parseObject(user, Memberr.class);
        Integer lessid = (Integer) redisTemplate.opsForValue().get("课程id");
        LambdaQueryWrapper<Mulu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Mulu::getMemberrId,memberr.getId()).eq(Mulu::getLessonnId,lessid);
        List<Mulu> list = muluService.list(wrapper);

        int size = list.size();

        return R.ok(size);
    }

    @GetMapping("tiao")
    public R tiao(Integer id){
        redisTemplate.opsForValue().set("课程id",id);
        return R.ok("ok");
    }


    @GetMapping("getCatalog")
    public R getCatalog(){
        List<Catalog> list = catalogService.list();
        List<Catalog> catalogs = PermissionHelper.buildPermission(list);
        return R.ok(catalogs);
    }



    @GetMapping("findAllZuire")
    public R findAllZuire(LessonnDto lessonnDto){

        try {
            PageHelper.startPage(lessonnDto.getPageNum(),lessonnDto.getPageSize());
            List<Lessonn> allZuire = lessonnService.findAllZuire(lessonnDto);
            PageInfo<Lessonn> pageInfo = new PageInfo<>(allZuire);
            return R.ok(pageInfo);

        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }

    }



    @GetMapping("findAllZuixing")
    public R findAllZuixing(LessonnDto lessonnDto){

        try {
            PageHelper.startPage(lessonnDto.getPageNum(),lessonnDto.getPageSize());
            List<Lessonn> allZuixing = lessonnService.findAllZuixing(lessonnDto);
            PageInfo<Lessonn> pageInfo = new PageInfo<>(allZuixing);
            return R.ok(pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }

    }


    @GetMapping("findAllList")
    public List<Coursee> findAllList(){
        return couressFegin.findAllList();
    }

    @GetMapping("findAllLessonn")
    public R findAllLessonn(LessonnDto lessonnDto){

        try {
            PageHelper.startPage(lessonnDto.getPageNum(),lessonnDto.getPageSize());
            List<Lessonn> list = lessonnService.findAllLessonn(lessonnDto);
            PageInfo<Lessonn> pageInfo = new PageInfo<>(list);
            return R.ok(pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }


//    @GetMapping("findAllLessonn")
//    public R findAllLessonn(@RequestBody LessonnDto lessonnDto){
//        return feginService.findAllLessonn(lessonnDto);
//    }


    @GetMapping("dz1")
    public R dz1(Integer lessonnId){
        LambdaQueryWrapper<Lessonn> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Lessonn::getId , lessonnId);
        Lessonn one = lessonnService.getOne(wrapper);
        one.setLikee(one.getLikee()+1);
        lessonnService.updateById(one);
        return R.ok("点赞成功");
    }

    @GetMapping("dz2")
    public R dz2(Integer lessonnId){
        LambdaQueryWrapper<Lessonn> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Lessonn::getId , lessonnId);
        Lessonn one = lessonnService.getOne(wrapper);
        one.setLikee(one.getLikee()+1);
        lessonnService.updateById(one);
        return R.ok("点赞成功");
    }

    @GetMapping("dz3")
    public R dz3(Integer lessonnId){
        LambdaQueryWrapper<Lessonn> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Lessonn::getId , lessonnId);
        Lessonn one = lessonnService.getOne(wrapper);
        one.setLikee(one.getLikee()+1);
        lessonnService.updateById(one);
        return R.ok("点赞成功");
    }

    @GetMapping("dz4")
    public R dz4(Integer lessonnId){
        LambdaQueryWrapper<Lessonn> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Lessonn::getId , lessonnId);
        Lessonn one = lessonnService.getOne(wrapper);
        one.setLikee(one.getLikee()+1);
        lessonnService.updateById(one);
        return R.ok("点赞成功");
    }



    @GetMapping("finless")
    public R finless(){
        return feginService.finless();
    }




    @GetMapping("findAllNum2")
    public R findAllNum2(){
        return trainnFeginService.findAllNum2();
    }


    @GetMapping("findAllNum1")
    public R findAllNum1(){
        return compFeginService.findAllNum1();
    }



    @GetMapping("findAllNum")
    public R findAllNum(){
        return feginService.findAllNum();
    }


    @GetMapping("findImg")
    public R findImg(){
        return memberrFeginService.findImg();
    }

    @GetMapping("findAllNum4")
    public R findAllNum4(){
        return feginService.findAllNum4();
    }

    @GetMapping("dz5")
    public R dz5(Integer lessonnId){
        LambdaQueryWrapper<Lessonn> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Lessonn::getId , lessonnId);
        Lessonn one = lessonnService.getOne(wrapper);

        String user = (String) redisTemplate.opsForValue().get("user");
        Memberr memberr = JSON.parseObject(user, Memberr.class);

        Object o = redisTemplate.opsForValue().get(lessonnId + memberr.getId());
        if (o ==null){
            one.setLikee(one.getLikee()+1);
            redisTemplate.opsForValue().set(lessonnId+memberr.getId(),one.getCount());
            lessonnService.updateById(one);
            return R.ok("点赞成功");
        }else {
            one.setLikee(one.getLikee()-1);
            redisTemplate.delete(lessonnId+memberr.getId());
            lessonnService.updateById(one);
            return R.ok("取消成功");
        }




    }

    @GetMapping("dz6")
    public R dz6(Integer lessonnId){
        LambdaQueryWrapper<Lessonn> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Lessonn::getId , lessonnId);
        Lessonn one = lessonnService.getOne(wrapper);

        String user = (String) redisTemplate.opsForValue().get("user");
        Memberr memberr = JSON.parseObject(user, Memberr.class);

        Object o = redisTemplate.opsForValue().get(lessonnId + memberr.getId());
        if (o ==null){
            one.setLikee(one.getLikee()+1);
            redisTemplate.opsForValue().set(lessonnId+memberr.getId(),one.getCount());
            lessonnService.updateById(one);
            return R.ok("点赞成功");
        }else {
            one.setLikee(one.getLikee()-1);
            redisTemplate.delete(lessonnId+memberr.getId());
            lessonnService.updateById(one);
            return R.ok("取消成功");
        }
    }

    @GetMapping("dz7")
    public R dz7(Integer lessonnId){
        LambdaQueryWrapper<Lessonn> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Lessonn::getId , lessonnId);
        Lessonn one = lessonnService.getOne(wrapper);

        String user = (String) redisTemplate.opsForValue().get("user");
        Memberr memberr = JSON.parseObject(user, Memberr.class);

        Object o = redisTemplate.opsForValue().get(lessonnId + memberr.getId());
        if (o ==null){
            one.setLikee(one.getLikee()+1);
            redisTemplate.opsForValue().set(lessonnId+memberr.getId(),one.getCount());
            lessonnService.updateById(one);
            return R.ok("点赞成功");
        }else {
            one.setLikee(one.getLikee()-1);
            redisTemplate.delete(lessonnId+memberr.getId());
            lessonnService.updateById(one);
            return R.ok("取消成功");
        }
    }

    @GetMapping("dz8")
    public R dz8(Integer lessonnId){
        LambdaQueryWrapper<Lessonn> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Lessonn::getId , lessonnId);
        Lessonn one = lessonnService.getOne(wrapper);

        String user = (String) redisTemplate.opsForValue().get("user");
        Memberr memberr = JSON.parseObject(user, Memberr.class);

        Object o = redisTemplate.opsForValue().get(lessonnId + memberr.getId());
        if (o ==null){
            one.setLikee(one.getLikee()+1);
            redisTemplate.opsForValue().set(lessonnId+memberr.getId(),one.getCount());
            lessonnService.updateById(one);
            return R.ok("点赞成功");
        }else {
            one.setLikee(one.getLikee()-1);
            redisTemplate.delete(lessonnId+memberr.getId());
            lessonnService.updateById(one);
            return R.ok("取消成功");
        }
    }

    @GetMapping("findAllTrann")
    public R findAllTrann(){
        return trainnFeginService.findAllTrann();
    }

    @GetMapping("findPaperr")
    public R findPaperr(){
        return paperrFeginService.findPaperr();
    }




















}
